# -*- coding:utf-8 -*-
'''
If we list all the natural numbers below 10 that are multiples of 3
or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the
sum of all the multiples of 3 or 5 below 1000.
题目的意思是先举例说明这里数列:[3,5,6,9] 10以内3或5的公倍数,之和是23
题目需要就1000以内3或5的公倍数之和.
 
这里使用了3种方法实现，分别是while 循环求和，列表求和以及生成器求和，
并比较3者之间的执行效率，发现生成器，效率高很多
程序输出：
common_divisor_3_or_5_sum_generator() value: 	 233168
(common_divisor_3_or_5_sum_for() value:			233168
common_divisor_3_or_5_sum_list() value:			233168
common_divisor_3_or_5_sum_generator() time is : 0.0002010753809261383
common_divisor_3_or_5_sum_for() time is : 0.12480264823723916
common_divisor_3_or_5_sum_list() time is : 0.11502318572910925

'''
import timeit

def common_divisor_3_or_5_sum_generator(n=1000):
    i, sum1 = 1, 0
    while i < n:
        if i % 3  == 0 or i % 5 ==0:
            sum1 += i
        i += 1
    yield sum1
    return "done"

def common_divisor_3_or_5_sum_for(n=1000):
    sum1,i = 0,0
    while i < n:
        if i % 3 ==0 or i % 5 ==0:
            sum1 += i
        i += 1
    return sum1

def common_divisor_3_or_5_sum_list(n=1000):
    common_divisor_3_or_5_list = []
    for i in range(1000):
        if i % 3 == 0 or i % 5 == 0:
            common_divisor_3_or_5_list.append(i)
    return sum(common_divisor_3_or_5_list)



if __name__ == '__main__':
    import  timeit

    for sum1 in common_divisor_3_or_5_sum_generator():
        print('common_divisor_3_or_5_sum_generator() value:','\t',sum1)

    # print('common_divisor_3_or_5_sum_for() value :','\t\t',common_divisor_3_or_5_sum_for())
    print('\t\t\t'.join(['(common_divisor_3_or_5_sum_for() value:',str(common_divisor_3_or_5_sum_for())]))
    # print('common_divisor_3_or_5_sum_list() value :',common_divisor_3_or_5_sum_list())
    print('%s\t\t\t%d' %('common_divisor_3_or_5_sum_list() value:',common_divisor_3_or_5_sum_list()))

    print('common_divisor_3_or_5_sum_generator() time is :',timeit.timeit('common_divisor_3_or_5_sum_generator()',setup='from __main__ import common_divisor_3_or_5_sum_generator',number=1000))
    print('common_divisor_3_or_5_sum_for() time is :',timeit.timeit('common_divisor_3_or_5_sum_for()',setup='from __main__ import common_divisor_3_or_5_sum_for',number=1000))
    print('common_divisor_3_or_5_sum_list() time is :',timeit.timeit('common_divisor_3_or_5_sum_list()',setup='from __main__ import common_divisor_3_or_5_sum_list',number=1000))
